//
//  FriendsTableViewController.m
//  好友列表
//
//  Created by 伍银荣 on 2021/6/8.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "FriendsTableViewController.h"
#import "Groups.h"
#import "FriendsCell.h"
#import "Friends.h"
#import "GroupsHeaderView.h"

@interface FriendsTableViewController ()<GroupsHeaderViewDelegate>
@property (nonatomic, strong) NSArray *groupsArr;

@end

@implementation FriendsTableViewController

-(NSArray *)groupsArr {
    if (!_groupsArr) {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            Groups *groupsModel = [Groups groupWithDictionary:dict];
            [mArr addObject:groupsModel];
        }
        _groupsArr = mArr;
    }
    return _groupsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.sectionHeaderHeight =50;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupsArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Groups *groups = self.groupsArr[section];
    if (groups.visible) {
        return groups.friends.count;
    } else {
        return 0;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Groups *groupsModel = self.groupsArr[indexPath.section];
    Friends *friendsModel = groupsModel.friends[indexPath.row];
    FriendsCell *cell = [FriendsCell friendsCellWithTableView:tableView];
    cell.friends = friendsModel;
    return cell;
}

//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return [self.groupsArr[section] name];
//}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GroupsHeaderView *headerView = [GroupsHeaderView groupsHeaderViewWithTableView:tableView];
    headerView.groups = self.groupsArr[section];
    headerView.delegate = self;
    headerView.tag = section;
    return headerView;
}


#pragma mark - GroupsHeaderViewDelegate
-(void)groupBtnClickedWithGtoupHeaderView:(GroupsHeaderView *)headerView {
    //刷新tableView
   // [self.tableView reloadData];
    //刷新具体点击的这一组
    NSIndexSet *idxSet = [[NSIndexSet alloc]initWithIndex:headerView.tag];
    //UITableViewRowAnimationFade:渐入渐出
    [self.tableView reloadSections:idxSet withRowAnimation:UITableViewRowAnimationFade];
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
